<?php
if( !defined('IN_INDEX') )
{
	printError("ACCESS DENIED", "This page is hidden.");
	exit;
}
include './inc/functions_like.php';

$task = @$_POST['status'];

switch($task){
	case 'like':
		like();break;
	case 'unlike':
		unlike();break;
	case 'show_users_like_workout':
		show_users_like_workout();break;
	case 'unlike_comment':
		unlike_comment();break;
	case 'like_comment':
		like_comment();break;
	case 'show_users_like_comment':
		show_users_like_comment();break;
	case 'following_user':
		following_user();break;
}

function following_user(){
	if(!is_follow_this_user($_POST['user_id']) ){
		add_this_user_to_my_followList($_POST['user_id']);
		echo  translate('not_follow_this_user');
	}else{
		remove_this_user_to_my_followList($_POST['user_id']);
		echo  translate('follow_this_user');
	}
	exit();
}

function show_users_like_comment(){
	global $db,$CONFIG;
	$post_id = intval($_POST['tid']);
	$sql= "SELECT * FROM tbl_like WHERE post_id=$post_id AND post_type='comment' AND comment_id=".intval($_POST['comment_id']);
	
	if(!$result = $db->sql_query($sql))
	{
		printerror("SQL ERROR", mysql_error($sql) ."<br />" . mysql_errno($sql));
	}
	
	$array_rc = array();
	while($tmp = $db->sql_fetchrow($result)){
		$array_rc[] = $tmp;
	}
	
	$ui = "<table>";
	foreach($array_rc as $val){
		$ui .= "<tr>";
		$USER_PROFILE = new User('id', $val['user_id']);
		if($USER_PROFILE->getAvatarpath() != "")
		{
			$profile_pic_path = $CONFIG['user_data_path'] . $USER_PROFILE->getId() . "/thumbs_" . $USER_PROFILE->getAvatarpath();
			if(file_exists($profile_pic_path))
			{
				$ui .= "<td><a href='index.php?action=profile&amp;id=" . $val['user_id'] . "'><img style='background: url(" . $profile_pic_path . ") no-repeat 50%; height: 60px; width: 60px;' src=\"" . $profile_pic_path . "\" alt=\"" . translate('profile_picture') . "\" /></a></td>";
				$ui .= "<td><a style='color:#555555;' href='index.php?action=profile&amp;id=".$val['user_id']."'>".htmlentities($USER_PROFILE->getFirstname()) . " " . ucf(htmlentities($USER_PROFILE->getLastname()))."</a></td>"; 
				$ui .= "<td>".ui_aj_follow_this_user($val['user_id'], $val['user_id'])."</td>";
			}
			else
			{
				$ui .= "<td>&nbsp;</td>";//$profile_pic_path;
				$ui .= "<td><a style='color:#555555;' href='index.php?action=profile&amp;id=".$val['user_id']."'>".htmlentities($USER_PROFILE->getFirstname()) . " " . ucf(htmlentities($USER_PROFILE->getLastname()))."</a></td>";
				$ui .= "<td>".ui_aj_follow_this_user($val['user_id'], $val['user_id'])."</td>";
			}
		}
		else
		{
			if($USER_PROFILE->getGender() == "female")
			{
				$ui .= "<td><a href='index.php?action=profile&amp;id=" . $val['user_id'] . "'><img src=\"" . $CONFIG['user_data_path'] . "profile_female.gif\" height='60' width='60' alt=\"" . translate('profile_picture') . "\" /></a></td>";
				$ui .= "<td><a style='color:#555555;' href='index.php?action=profile&amp;id=".$val['user_id']."'>".htmlentities($USER_PROFILE->getFirstname()) . " " . ucf(htmlentities($USER_PROFILE->getLastname()))."</a></td>";
				$ui .= "<td>".ui_aj_follow_this_user($val['user_id'], $val['user_id'])."</td>";
			}
			else
			{
				$ui .= "<td><a href='index.php?action=profile&amp;id=" . $val['user_id'] . "'><img src=\"" . $CONFIG['user_data_path'] . "profile_male.gif\" height='60' width='60' alt=\"" . translate('profile_picture') . "\" /></a></td>";
				$ui .= "<td><a style='color:#555555;' href='index.php?action=profile&amp;id=".$val['user_id']."'>".htmlentities($USER_PROFILE->getFirstname()) . " " . ucf(htmlentities($USER_PROFILE->getLastname()))."</a></td>";
				$ui .= "<td>".ui_aj_follow_this_user($val['user_id'], $val['user_id'])."</td>";
			}
		}
	}
	
	echo $ui;
	exit();
}

function show_users_like_workout(){
	global $db,$CONFIG;
	$post_id = intval($_POST['tid']);
	$sql= "SELECT * FROM tbl_like WHERE post_id=$post_id AND post_type='training'";
	
	if(!$result = $db->sql_query($sql))
	{
		printerror("SQL ERROR", mysql_error($sql) ."<br />" . mysql_errno($sql));
	}
	
	$array_rc = array();
	while($tmp = $db->sql_fetchrow($result)){
		$array_rc[] = $tmp;
	}
	
	$ui = "<table>";
	foreach($array_rc as $val){
		$ui .= "<tr>";
		$USER_PROFILE = new User('id', $val['user_id']);
		if($USER_PROFILE->getAvatarpath() != "")
		{
			$profile_pic_path = $CONFIG['user_data_path'] . $USER_PROFILE->getId() . "/thumbs_" . $USER_PROFILE->getAvatarpath();
			if(file_exists($profile_pic_path))
			{
				$ui .= "<td><a href='index.php?action=profile&amp;id=" . $val['user_id'] . "'><img style='background: url(" . $profile_pic_path . ") no-repeat 50%; height: 60px; width: 60px;' src=\"" . $profile_pic_path . "\" alt=\"" . translate('profile_picture') . "\" /></a></td>";
				$ui .= "<td><a style='color:#555555;' href='index.php?action=profile&amp;id=".$val['user_id']."'>".htmlentities($USER_PROFILE->getFirstname()) . " " . ucf(htmlentities($USER_PROFILE->getLastname()))."</a></td>"; 
				$ui .= "<td>".ui_aj_follow_this_user($val['user_id'], $val['user_id'])."</td>";
			}
			else
			{
				$ui .= "<td>&nbsp;</td>";//$profile_pic_path;
				$ui .= "<td><a style='color:#555555;' href='index.php?action=profile&amp;id=".$val['user_id']."'>".htmlentities($USER_PROFILE->getFirstname()) . " " . ucf(htmlentities($USER_PROFILE->getLastname()))."</a></td>";
				$ui .= "<td>".ui_aj_follow_this_user($val['user_id'], $val['user_id'])."</td>";
			}
		}
		else
		{
			if($USER_PROFILE->getGender() == "female")
			{
				$ui .= "<td><a href='index.php?action=profile&amp;id=" . $val['user_id'] . "'><img src=\"" . $CONFIG['user_data_path'] . "profile_female.gif\" height='60' width='60' alt=\"" . translate('profile_picture') . "\" /></a></td>";
				$ui .= "<td><a style='color:#555555;' href='index.php?action=profile&amp;id=".$val['user_id']."'>".htmlentities($USER_PROFILE->getFirstname()) . " " . ucf(htmlentities($USER_PROFILE->getLastname()))."</a></td>";
				$ui .= "<td>".ui_aj_follow_this_user($val['user_id'], $val['user_id'])."</td>";
			}
			else
			{
				$ui .= "<td><a href='index.php?action=profile&amp;id=" . $val['user_id'] . "'><img src=\"" . $CONFIG['user_data_path'] . "profile_male.gif\" height='60' width='60' alt=\"" . translate('profile_picture') . "\" /></a></td>";
				$ui .= "<td><a style='color:#555555;' href='index.php?action=profile&amp;id=".$val['user_id']."'>".htmlentities($USER_PROFILE->getFirstname()) . " " . ucf(htmlentities($USER_PROFILE->getLastname()))."</a></td>";
				$ui .= "<td>".ui_aj_follow_this_user($val['user_id'], $val['user_id'])."</td>";
			}
		}
	}
	
	echo $ui;
	exit();
}

function like_comment(){
	global $db;
	
	$sql = "INSERT INTO tbl_like SET post_id='".intval($_POST['training_id'])."' ,".
			"post_type='comment', ".
			"user_id='".intval($_POST['my_id'])."', ".
			"like_date='".time()."', ".
			"owner_id='".intval($_POST['owner_id'])."', ".
			"comment_id='".intval($_POST['comment_id'])."' ";
			
	if(!$result = $db->sql_query($sql))
	{
		printerror("SQL ERROR", mysql_error($sql) ."<br />" . mysql_errno($sql));
	}
	$number_persons_like = count_numberOfPersons_like_comment($owner_id=intval($_POST['owner_id']), $post_id=intval($_POST['training_id']), intval($_POST['comment_id']));
	
	echo " ".$number_persons_like." ". 
	"<a href='javascript:void(0);' onclick=\"like_unlike_comment('unlike_comment',{$_POST['my_id']},{$_POST['owner_id']},{$_POST['training_id']},{$_POST['comment_id']});\" >".
	translate('unlike')."</a>";
	exit();
}

function unlike_comment(){
	global $db;
	
	$sql = "DELETE FROM tbl_like WHERE post_id='".intval($_POST['training_id'])."' AND ".
		   "post_type='comment' AND ".
		   "user_id='".intval($_POST['my_id'])."' AND ".	
		   "owner_id='".intval($_POST['owner_id'])."' AND ".
		   "comment_id='".intval($_POST['comment_id'])."' ";
	
	if(!$result = $db->sql_query($sql))
	{
		printerror("SQL ERROR", mysql_error($sql) ."<br />" . mysql_errno($sql));
	}
	$number_persons_like = count_numberOfPersons_like_comment($owner_id=intval($_POST['owner_id']), $post_id=intval($_POST['training_id']), intval($_POST['comment_id']));
	
	echo " ".$number_persons_like." ".  
	"<a href='javascript:void(0);' onclick=\"like_unlike('like_comment',{$_POST['my_id']},{$_POST['owner_id']},{$_POST['training_id']},{$_POST['comment_id']});\" >".
	translate('like')."</a>";
	exit();	
}

function like(){
	global $db;
	
	$sql = "INSERT INTO tbl_like SET post_id='".intval($_POST['training_id'])."' ,".
			"post_type='training', ".
			"user_id='".intval($_POST['my_id'])."', ".
			"like_date='".time()."', ".
			"owner_id='".intval($_POST['owner_id'])."'";
	if(!$result = $db->sql_query($sql))
	{
		printerror("SQL ERROR", mysql_error($sql) ."<br />" . mysql_errno($sql));
	}
	$number_persons_like = count_numberOfPersons_like($owner_id=intval($_POST['owner_id']), $post_id=intval($_POST['training_id']));
	
	echo " ".$number_persons_like." ". 
	"<a href='javascript:void(0);' onclick=\"like_unlike('unlike',{$_POST['my_id']},{$_POST['owner_id']},{$_POST['training_id']});\" >".
	translate('unlike')."</a>";
	exit();
}

function unlike(){
	global $db;
	
	$sql = "DELETE FROM tbl_like WHERE post_id='".intval($_POST['training_id'])."' AND ".
		   "post_type='training' AND ".
		   "user_id='".intval($_POST['my_id'])."' AND ".	
		   "owner_id='".intval($_POST['owner_id'])."'";
	
	if(!$result = $db->sql_query($sql))
	{
		printerror("SQL ERROR", mysql_error($sql) ."<br />" . mysql_errno($sql));
	}
	$number_persons_like = count_numberOfPersons_like($owner_id=intval($_POST['owner_id']), $post_id=intval($_POST['training_id']));
	
	echo " ".$number_persons_like." ".  
	"<a href='javascript:void(0);' onclick=\"like_unlike('like',{$_POST['my_id']},{$_POST['owner_id']},{$_POST['training_id']});\" >".
	translate('like')."</a>";
	exit();	
}